package StudyCode.summary.mycollection;

import java.lang.reflect.Array;
import java.util.Collection;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ConvertsACollectionToAPrimitiveArrayOfAGivenType {
	public static void main(String[] argv) {
		Class type = String.class;
		Collection collection = java.util.Arrays.asList("asdf",
				"www.tah1986.com");
		System.out.println(java.util.Arrays.toString(toArray(type,
				collection)));
	}

	public static <T> T[] toArray(Class<T> type,
	                              Collection<? extends T> collection) {
		T[] array = (T[]) Array.newInstance(type, collection.size());
		int i = 0;
		for (T item : collection) {
			array[i++] = item;
		}

		return array;
	}
}
